package com.zdjx.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.zdjx.pojo.ResponseResult;
import com.zdjx.pojo.SceneResult;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

public class JDBCUtil {

    static String driverClass = null;
    static String url = null;
    static String username = null;
    static String password = null;
    static DruidDataSource  dataSource = new DruidDataSource();
    static Connection conn = null;

    static{
        Properties p = new Properties();
        try{
            InputStream is = new FileInputStream("src/main/resources/db.properties");
            p.load(is);
            driverClass = p.getProperty("driverClassName");
            url = p.getProperty("url");
            username = p.getProperty("username");
            password = p.getProperty("password");

            dataSource.setDriverClassName(driverClass);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);

            conn = dataSource.getConnection();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void saveResponseResult(ResponseResult rr){
        String sql = "INSERT INTO j_res (requestURL, saveTime, result, exeTime) VALUES (?, ?, ?, ?)";
        try{
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, rr.getRequestURL());
            ptmt.setObject(2, new Date());
            ptmt.setString(3, rr.getResult());
            ptmt.setLong(4, rr.getExeTime());
            ptmt.execute();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static void saveSceneResult(SceneResult sr){
        String sql = "INSERT INTO j_sce (saveTime, exeTime, resu, rids)VALUES (?, ?, ?, ?)";
        try{
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setObject(1, new Date());
            ptmt.setLong(2, sr.getExeTime());
            ptmt.setBoolean(3, sr.getResu());
            ptmt.setString(4, sr.getRids().toString());
            ptmt.execute();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }


}
